package org.ragdollcat.first.manager;

import cn.hutool.core.io.FileUtil;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.ciModel.persistence.PicOperations;
import lombok.extern.slf4j.Slf4j;
import org.ragdollcat.first.config.CosClientConfig;
import org.ragdollcat.first.util.StringHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;

/**
 * 文件上传，下载，解析通用的manager
 * 与业务逻辑作区分
 */
@Slf4j
@Component
public class CosManager {

    @Resource
    private CosClientConfig cosClientConfig;

    @Resource
    private COSClient cosClient;

    /**
     * 上传文件
     *
     * @param filePath 文件路径/文件名
     * @param file     文件对象
     * @return
     */
    public PutObjectResult putObject(String filePath, File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), filePath, file);
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * 下载文件
     *
     * @param filePath 文件路径
     * @return
     */
    public COSObject getObject(String filePath) {
        GetObjectRequest getObjectRequest = new GetObjectRequest(cosClientConfig.getBucket(), filePath);
        return cosClient.getObject(getObjectRequest);
    }

    /**
     * 单个删除文件
     *
     * @param filePath
     */
    public void delObject(String filePath) {
        try {
            //在对象的访问地址examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/folder/picture.jpg 中，对象键为folder/picture.jpg
            cosClient.deleteObject(cosClientConfig.getBucket(), StringHandler.extractPathAfterDomain(filePath));
        } catch (CosClientException e) {
            log.error("图片删除失败，路径：{}，原因：", filePath, e);
        }
    }


    /**
     * 利用数据万象解析图片
     *
     * @param filePath 例：/test/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20240617214942.jpg
     * @param file     文件对象
     * @return 处理结果
     */
    public PutObjectResult putPictureObject(String filePath, File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), filePath, file);
        //利用数据万象解析图片，否则拿不到图片信息
        PicOperations picOperations = new PicOperations();
        picOperations.setIsPicInfo(1);

        //图片格式转换webp
        ArrayList<PicOperations.Rule> rules = new ArrayList<>();
        PicOperations.Rule rule = new PicOperations.Rule();
        rule.setBucket(cosClientConfig.getBucket());
        rule.setRule("imageMogr2/format/webp");
        rule.setFileId(FileUtil.mainName(filePath) + ".webp");
        rules.add(rule);

        //图片大小大于2MB时才进行压缩
//        if (file.length() > 2 * 1024) {
        //图片缩略图规则
        PicOperations.Rule thumbnailRule = new PicOperations.Rule();
        thumbnailRule.setBucket(cosClientConfig.getBucket());
        thumbnailRule.setRule(String.format("imageMogr2/thumbnail/%sx%s>", 256, 256));
        thumbnailRule.setFileId(FileUtil.mainName(filePath) + "_thumbnail." + FileUtil.getSuffix(filePath));
        rules.add(thumbnailRule);
//        }


        picOperations.setRules(rules);
        putObjectRequest.setPicOperations(picOperations);
        return cosClient.putObject(putObjectRequest);
    }

}
